package com.sad.jetpack.v1.thememanager.api;

import android.content.Context;
import android.content.res.TypedArray;

import androidx.annotation.AttrRes;
import androidx.annotation.StyleableRes;

import java.util.ArrayList;
import java.util.List;

public class DefaultThemeAttrDynamicPerformer implements IThemeAttrDynamicPerformer{

    //private @StyleRes int themeResId=0;
    private @StyleableRes int[] styleAble;//属性集合
    private List<Integer> attrs=new ArrayList<>();
    private Context context;
    public DefaultThemeAttrDynamicPerformer(Context context){
        this.context=context;
        //this.themeResId=themeResId;
    }

    @Override
    public IThemeAttrDynamicPerformer styleAble(@StyleableRes int[] styleAble) {
        this.styleAble=styleAble;
        return this;
    }

    @Override
    public IThemeAttrDynamicPerformer addAttr(@AttrRes int... attrs) {
        if (attrs!=null){
            for (int attr:attrs
                 ) {
                this.attrs.add(attr);
            }
        }
        return this;
    }
    @Override
    public TypedArray getTypeArray() {
        TypedArray ta=null;
        try {
            if (styleAble!=null && styleAble.length>0){
                ta=context.obtainStyledAttributes(styleAble);
            }
            else if (attrs!=null && attrs.size()>0){
                int[] ats=new int[attrs.size()];
                for (int i = 0; i < attrs.size(); i++) {
                    ats[i]=attrs.get(i);
                }
                ta=context.obtainStyledAttributes(ats);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ta;
    }

    @Override
    public TypedArray getTypeArrayFromCurrThemeDes() {
        TypedArray ta=null;
        try {
            int resId= BasicThemeDesHelper.findResIdByLocalTheme(LocalTheme.getLastLocalTheme(context));
            if (styleAble!=null && styleAble.length>0){
                ta=context.obtainStyledAttributes(resId,styleAble);
            }
            else if (attrs!=null && attrs.size()>0){
                int[] ats=new int[attrs.size()];
                for (int i = 0; i < attrs.size(); i++) {
                    ats[i]=attrs.get(i);
                }
                ta=context.obtainStyledAttributes(resId,ats);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ta;
    }
}
